package com.iot.inclinometer.pojo;

import android.util.Log;

import com.iot.inclinometer.util.JsonUtil;
import com.xuhao.didi.core.iocore.interfaces.ISendable;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * 单条钻孔数据结构体
 */
public class SendDataDTO implements ISendable {
    private final static String TAG = SendDataDTO.class.getSimpleName();
    public static String COMMAND_PREFIX = "JGKJ";
    public static String COMMAND_SUFFIX = "#!";


    /**
     * 数据类型唯一标识 key:a
     */
    private String authId = "";

    /**
     * 深部位移key:b
     */
    private String dataType = "sbwy";

    /**
     * 项目名称 key:c
     */
    private String projectName = "";
    /**
     * 工点名称 key:d
     */
    private String workPointName = "";
    /**
     * 数据总条数 key:e
     */
    private int dataCount = 0;

    /**
     * 数据体 key:f
     */
    private DataBodyDTO dataBody = null;

    public int getUploaded() {
        return uploaded;
    }

    public void setUploaded(int uploaded) {
        this.uploaded = uploaded;
    }

    private transient volatile int uploaded = -1;


    public SendDataDTO() {
        this(null, null, null);
    }


    public SendDataDTO(String prjName, String wpName, String drillName) {
        //北京久感科技有限公司
        this.projectName = prjName == null ? "" : prjName;
        this.workPointName = wpName == null ? "" : wpName;
        this.authId = stringToUnicode(this.projectName + this.workPointName + this.dataType);

        dataBody = new DataBodyDTO(drillName, null);

        this.dataCount = 0;
    }

    /**
     * 字符串转换unicode
     */
    public static String stringToUnicode(String string) {
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            // 取出每一个字符
            char c = string.charAt(i);
            // 转换为unicode
            unicode.append(Integer.toHexString(c));
        }
        return unicode.toString();
    }


    public int calcCount() {
        this.dataCount = dataBody.getData().size();

        return this.dataCount;
    }



    @Override
    public byte[] parse() {
        //计算数据总条数
        calcCount();

        String str = this.toJson();
        Log.i(TAG, str);
        //根据服务器的解析规则,构建byte数组
        byte[] body = str.getBytes(Charset.defaultCharset());
        ByteBuffer bb = ByteBuffer.allocate(/*4 + */body.length);
        bb.order(ByteOrder.BIG_ENDIAN);
        //bb.putInt(body.length);
        bb.put(body);
        return bb.array();
    }

    /**
     * 转换为JSON串
     *
     * @return
     */
    public String toJson() {
        String r = "";
        try {
            r = JsonUtil.toJsonString(this);
        } catch (Exception exe) {
            Log.e(TAG, "converto json failed! >> " + exe.getLocalizedMessage());
            r = "{}";
        }

        return COMMAND_PREFIX + r + COMMAND_SUFFIX;
    }

    public SendDataDTO clone() {
        SendDataDTO obj = new SendDataDTO(this.getProjectName(), this.getWorkpointName(), this.getDrillName());
        obj.setTranData(this.getTranData().clone());

        return obj;
    }

    public String getIdentity() {
        return authId;
    }

    public void setIdentity(String dataIdentity) {
        this.authId = dataIdentity;
    }


    public String getType() {
        return dataType;
    }

    public void setType(String dataType) {
        this.dataType = dataType;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getWorkpointName() {
        return workPointName;
    }

    public void setWorkpointName(String workpointName) {
        this.workPointName = workpointName;
    }


    public int getCount() {
        return dataCount;
    }

    public void setCount(int e) {
        this.dataCount = e;
    }


    public DataBodyDTO getTranData() {
        return dataBody;
    }

    public void setTranData(DataBodyDTO dataBody) {
        this.dataBody = dataBody;
    }

    public void addDataItem(DataBodyItemDTO item) {
        dataBody.addDataItem(item);
        calcCount();
    }

    public String getDrillName() {
        return this.dataBody.getDrillName();
    }

    public void setDrillName(String v) {
        this.dataBody.setDrillName(v);
    }


    /**
     * 数据实体，钻孔对象
     */
    public class DataBodyDTO {
        /**
         * 钻孔号 key:a
         */
        private String pointName = "";

        /**
         * 钻孔数据 key:b
         */
        private List<DataBodyItemDTO> detailList;

        public DataBodyDTO() {
            this(null, null);
        }

        public DataBodyDTO(String drillNo, List<DataBodyItemDTO> data) {
            this.pointName = drillNo == null ? "" : drillNo;
            this.detailList = data;
            if (this.detailList == null) {
                this.detailList = new ArrayList<>();
            }
        }


        public void addDataItem(DataBodyItemDTO item) {
            detailList.add(item);
        }


        public String getDrillName() {
            return pointName;
        }

        public void setDrillName(String pointName) {
            this.pointName = pointName;
        }

        public List<DataBodyItemDTO> getData() {
            return detailList;
        }

        public void setData(List<DataBodyItemDTO> listdata) {
            this.detailList = listdata;
        }

        public DataBodyDTO clone() {
            DataBodyDTO dataBodyDTO = new DataBodyDTO(this.getDrillName(), null);

            for (DataBodyItemDTO item : this.getData()) {
                dataBodyDTO.addDataItem(item.clone());
            }

            return dataBodyDTO;
        }
    }

    /**
     * 数据实体单条数据
     */
    public static class DataBodyItemDTO {

        /**
         * 采集时间 key:a
         */
        private String collectionTime = "";

        /**
         * 钻孔数据 key:b
         */
        private List<DataBodyItemNodeDTO> dataList;

        public DataBodyItemDTO() {
            this(null, null);
        }

        public DataBodyItemDTO(String time, List<DataBodyItemNodeDTO> data) {
            this.collectionTime = time == null ? "" : time;
            this.dataList = data;

            if (this.dataList == null) {
                this.dataList = new ArrayList<>();
            }
        }

        public void addDataItem(DataBodyItemNodeDTO item) {
            dataList.add(item);
        }

        public String getCollectTime() {
            return collectionTime;
        }

        public void setCollectTime(String time) {
            this.collectionTime = time;
        }


        public List<DataBodyItemNodeDTO> getData() {
            return dataList;
        }

        public void setData(List<DataBodyItemNodeDTO> detailList) {
            detailList = detailList;
        }


        public DataBodyItemDTO clone() {
            DataBodyItemDTO dataBodyItemDTO = new DataBodyItemDTO(this.getCollectTime(), null);

            for (DataBodyItemNodeDTO item : this.getData()) {
                dataBodyItemDTO.addDataItem(item.clone());
            }

            return dataBodyItemDTO;
        }
    }


    /**
     * 数据实体单条数据
     */
    public static class DataBodyItemNodeDTO {
        /**
         * 深度:key a
         */
        private double depth;
        /**
         * 角度A0 方向：key b
         */
        private double xAngle;
        /**
         * 角度B0 方向：key c
         */
        private double yAngle;

        /**
         * 角度A180 方向,暂时没用到，不上传
         */
        private transient double A180;

        /**
         * 角度B180 方向,暂时没用到，不上传
         */
        private transient double B180;

        public DataBodyItemNodeDTO() {
        }

        public DataBodyItemNodeDTO(double depth, double a0, double b0) {
            this(depth, a0, b0, 0, 0);
        }

        public DataBodyItemNodeDTO(double depth, double a0, double b0, double a180, double b180) {
            this.depth = depth;
            this.xAngle = a0;
            this.yAngle = b0;
            this.A180 = a180;
            this.B180 = b180;
        }

        public double getDepth() {
            return depth;
        }

        public void setDepth(double depth) {
            this.depth = depth;
        }

        public double getxAngle() {
            return xAngle;
        }

        public void setxAngle(double xAngle) {
            this.xAngle = xAngle;
        }

        public double getyAngle() {
            return yAngle;
        }

        public void setyAngle(double yAngle) {
            this.yAngle = yAngle;
        }

        public double getA180() {
            return A180;
        }

        public void setA180(double a180) {
            A180 = a180;
        }

        public double getB180() {
            return B180;
        }

        public void setB180(double b180) {
            B180 = b180;
        }


        public DataBodyItemNodeDTO clone() {

            return new DataBodyItemNodeDTO(this.getDepth(), this.getxAngle(), this.getyAngle(), this.getA180(), this.getB180());
        }
    }
}